package com.beinhospital_sw21_h2_05.beinhospitalsw21h205.service.impl;

import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.Admission;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.MedicalOrder;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.mapper.PatientMapper;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.service.PatientService;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.utils.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    PatientMapper patientMapper;

    /**
     * 分页查询患者的入院信息
     *
     * @param page     当前页码
     * @param pageSize 每页条数
     * @param Flag     查询条件，入院信息的状态标志
     * @param UserID   用户ID，用于获取患者ID
     * @return 分页结果
     */
    @Override
    public PageBean page(Integer page, Integer pageSize, Integer Flag, Integer UserID) {
        // 获取患者ID
        Integer PatientID = patientMapper.getPatientID(UserID);
        // 使用PageHelper进行分页
        PageHelper.startPage(page, pageSize);
        // 查询患者入院信息列表
        List<Admission> admissionList = patientMapper.list(Flag, PatientID);
        // 将查询结果包装成PageBean
        Page<Admission> p = (Page<Admission>) admissionList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    /**
     * 添加患者入院信息
     *
     * @param admission 患者入院信息对象
     * @param UserID    用户ID，用于获取患者ID
     */
    @Override
    public void add(Admission admission, Integer UserID) {
        // 获取患者ID
        Integer PatientID = patientMapper.getPatientID(UserID);
        // 设置入院信息中的患者ID、科室ID和状态标志
        admission.setDepartmentID(patientMapper.getDepartmenID(admission.getDepartmentName()));
        admission.setPatientID(PatientID);
        admission.setFlag(0);
        // 调用Mapper进行患者入院信息添加操作
        patientMapper.add(admission);
    }

    /**
     * 分页查询患者的医嘱信息
     *
     * @param page   当前页码
     * @param pageSize 每页条数
     * @param UserID 用户ID，用于获取患者ID
     * @return 分页结果
     */
    @Override
    public PageBean page1(Integer page, Integer pageSize, Integer UserID) {
        // 获取患者ID
        Integer PatientID = patientMapper.getPatientID(UserID);
        // 使用PageHelper进行分页
        PageHelper.startPage(page, pageSize);
        // 查询患者医嘱信息列表
        List<MedicalOrder> medicalOrderList = patientMapper.list1(PatientID);
        // 将查询结果包装成PageBean
        Page<MedicalOrder> p = (Page<MedicalOrder>) medicalOrderList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }
}
